let Main = {
    baseUrl: 'http://81.71.65.4:3003',
    uri: location.href,
    /**
     * 商品详情渲染模块
     */
    renderShopPart(id) {
        let html = ''
        axios.post(this.baseUrl + '/product/detail', { productId: id }).then((res) => {
            let data = res.data.result
                // console.log(data);
            document.querySelector('#sw').innerHTML = `<div><img src="${data.banners[0]}"></div>`
            document.querySelector('.he b').innerHTML = `${data.masterName}`
            document.querySelector('.ce').innerHTML = `${data.slaveName}`
            document.querySelector('.lf2').innerHTML = `${data.minPrice /100}`
            document.querySelector('.lf3').innerHTML = `￥${data.maxPrice / 100}`
            let html = ''
            let txt = ` <div class="xq-top">————商品详情————</div>`
            data.descPics.forEach(function(v, i) {
                html += `<img src="${v}">`
            })
            document.querySelector('.xiangq').innerHTML = txt + html
        })
    },
    addShopp(id) {
        //获取到本地的存储
        let token = localStorage.getItem('token')
        axios.interceptors.request.use(config => {
            if (token) {
                config.headers['user-token'] = token
            }
            return config
        }, error => { return Promise.reject(error) })

        axios.post(this.baseUrl + '/cart/add', { productId: id }).then((res) => {
            console.log(res);
            this.renderCart()
        }).catch(() => {
            console.log('请求错误!');
        })
    },
    renderCart() {
        let token = localStorage.getItem('token')
        axios.post(this.baseUrl + '/cart/all', { token: token }).then((res) => {
            let num = res.data.list.length
            document.querySelector('.box1 a .num').innerText = num
        })
    }
}
onload = () => {
    let id = Main.uri.substring(Main.uri.indexOf("sid=") + 4)
    Main.renderShopPart(id)

    document.querySelector('.btn .addShop').addEventListener('click', () => {
        Main.addShopp(id)

    })

    Main.renderCart()
}